<?php
/**
 * Created by PhpStorm.
 * User: 冰封~零度
 * Date: 2018/10/11
 * Time: 10:26
 */

namespace app\mobile\controller;

use think\Controller;
use think\Db;
use think\Session;

class Index extends Controller
{
    // 首页
    public function Index()
    {
        // 判断用户是否登录
        $puser = Session::get('puser');
        // 查询问题分类
        $allCate = Db::table('zz_cate')->select();

        // 查询 用户信息 问题信息 问题评论
        $userProblem = Db::table(['zz_user'=>'u','zz_problem'=>'p','zz_cate'=>'c'])
                    ->where('u.zz_id = p.zz_uid')
                    ->where('p.zz_problem_cid = c.zz_cate_id')
                    ->select();

        // 查询问题评论
        $problemComment = Db::table('zz_comment')->select();

        for ($i=0;$i<sizeof($problemComment);$i++){
            for ($j=0;$j<sizeof($userProblem);$j++){
                if (!empty($userProblem[$j]['zz_problem_id'])){
                    if ($problemComment[$i]['zz_problem_id'] == $userProblem[$j]['zz_problem_id']){
                        $userComment[$userProblem[$j]['zz_problem_id']][] = $problemComment[$i]['zz_problem_id'];
                    } else if(empty($userComment[$userProblem[$j]['zz_problem_id']])){
                        $userComment[$userProblem[$j]['zz_problem_id']] = null;
                    }
                }
            }
        }

        return view('index/index',[
            'allCate'     => $allCate,
            'userProblem' => $userProblem,
            'userComment' => $userComment,
            'puser'       => $puser
        ]);
    }

    public function ToIndex($name)
    {
        // 判断用户是否登录
        $puser = Session::get('puser');
        // 查询问题分类
        $allCate = Db::table('zz_cate')->select();

        // 查询 用户信息 问题信息 问题评论
        $userProblem = Db::table(['zz_user'=>'u','zz_problem'=>'p','zz_cate'=>'c'])
            ->where('u.zz_id = p.zz_uid')
            ->where('p.zz_problem_cid = c.zz_cate_id')
            ->where('c.zz_cate_name',$name)
            ->select();

        // 查询问题评论
        $problemComment = Db::table('zz_comment')->select();

        for ($i=0;$i<sizeof($problemComment);$i++){
            for ($j=0;$j<sizeof($userProblem);$j++){
                if (!empty($userProblem[$j]['zz_problem_id'])){
                    if ($problemComment[$i]['zz_problem_id'] == $userProblem[$j]['zz_problem_id']){
                        $userComment[$userProblem[$j]['zz_problem_id']][] = $problemComment[$i]['zz_problem_id'];
                    } else if(empty($userComment[$userProblem[$j]['zz_problem_id']])){
                        $userComment[$userProblem[$j]['zz_problem_id']] = null;
                    }
                }
            }
        }

        // 如果当前问题下没有任何内容，做标记
        if (empty($userProblem)){
            $userComment= 1;
        }

        return view('index/toIndex',[
            'allCate'     => $allCate,
            'userProblem' => $userProblem,
            'userComment' => $userComment,
            'name'        => $name,
            'puser'       => $puser
        ]);
    }
}